home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / search / lsmsearc.tar / lsmsearc / lsmsearch / dialog.pl next >
Text File  |  1995-07-12  |  11KB  |  448 lines

  1. # Functions that handle calling dialog(1) -*-perl-*-
  2.  
  3. # Return values are 1 for success and 0 for failure (or cancel)
  4. # Resultant text (if any) is in dialog_result
  5.  
  6. # Unfortunately, the guage requires use of /bin/sh to get going.
  7. # I didn't bother to make the others shell-free, although it
  8. # would be simple to do.
  9.  
  10. # Note that dialog generally returns 0 for success, so I invert the
  11. # sense of the return code for more readable boolean expressions.
  12.  
  13. $scr_lines = 24;
  14.  
  15. require "flush.pl";
  16.  
  17. sub rhs_clear {
  18.     return system("dialog --clear");
  19. }
  20.  
  21. sub rhs_textbox {
  22.     local ( $title, $file, $width, $height ) = @_;
  23.  
  24.     system("dialog --title \"$title\" --textbox $file $height $width");
  25.  
  26.     return 1;
  27. }
  28.  
  29. sub rhs_msgbox {
  30.     local ( $title, $message, $width ) = @_;
  31.     local ( $tmp, $height, $message_len );
  32.  
  33.     $message = &rhs_wordwrap($message, $width);
  34.     $message_len = split(/^/, $message);
  35.     $tmp = $message;
  36.     if (chop($tmp) eq "\n") {
  37.     $message_len++;
  38.     }
  39.     $height = 4 + $message_len;
  40.  
  41.     $tmp = system("dialog --title \"$title\" --msgbox \"$message\" $height $width");
  42.     if ($tmp) {
  43.     return 0;
  44.     } else {
  45.     return 1;
  46.     }
  47. }
  48.  
  49. sub rhs_infobox {
  50.     local ( $title, $message, $width ) = @_;
  51.     local ( $tmp, $height, $message_len );
  52.  
  53.     $message = &rhs_wordwrap($message, $width);
  54.     $message_len = split(/^/, $message);
  55.     $tmp = $message;
  56.     if (chop($tmp) eq "\n") {
  57.     $message_len++;
  58.     }
  59.     $height = 2 + $message_len;
  60.  
  61.     return system("dialog --title \"$title\" --infobox \"$message\" $height $width");
  62. }
  63.  
  64. sub rhs_yesno {
  65.     local ( $title, $message, $width ) = @_;
  66.     local ( $tmp, $height, $message_len );
  67.  
  68.     $message = &rhs_wordwrap($message, $width);
  69.     $message_len = split(/^/, $message);
  70.     $tmp = $message;
  71.     if (chop($tmp) eq "\n") {
  72.     $message_len++;
  73.     }
  74.     $height = 4 + $message_len;
  75.  
  76.     $tmp = system("dialog --title \"$title\" --yesno \"$message\" $height $width");
  77.     # Dumb: dialog returns 0 for "yes" and 1 for "no"
  78.     if (! $tmp) {
  79.     return 1;
  80.     } else {
  81.     return 0;
  82.     }
  83. }
  84.  
  85. sub rhs_guage {
  86.     local ( $title, $message, $width, $percent ) = @_;
  87.     local ( $tmp, $height, $message_len );
  88.  
  89.     $guage_width = $width;
  90.  
  91.     $message = &rhs_wordwrap($message, $width);
  92.     $message_len = split(/^/, $message);
  93.     $tmp = $message;
  94.     if (chop($tmp) eq "\n") {
  95.     $message_len++;
  96.     }
  97.     $height = 5 + $message_len;
  98.  
  99.     open(GUAGE, "|dialog --title \"$title\" --guage \"$message\" $height $width $percent");
  100. }
  101.  
  102. sub rhs_update_guage {
  103.     local ( $percent ) = @_;
  104.  
  105.     &printflush(GUAGE, "$percent\n");
  106. }
  107.  
  108. sub rhs_update_guage_and_message {
  109.     local ( $message, $percent ) = @_;
  110.  
  111.     $message = &rhs_wordwrap($message, $guage_width);
  112.     $message =~ s/\n/\\n/g;
  113.     &printflush(GUAGE, "XXX\n$percent\n$message\nXXX\n");
  114. }
  115.  
  116. sub rhs_stop_guage {
  117.     close GUAGE;
  118. }
  119.  
  120. sub rhs_inputbox {
  121.     local ( $title, $message, $width, $instr ) = @_;
  122.     local ( $tmp, $height, $message_len );
  123.  
  124.     $message = &rhs_wordwrap($message, $width);
  125.     $message_len = split(/^/, $message);
  126.     $tmp = $message;
  127.     if (chop($tmp) eq "\n") {
  128.     $message_len++;
  129.     }
  130.     $height = 7 + $message_len;
  131.  
  132.     return &return_output(0, "dialog --title \"$title\" --inputbox \"$message\" $height $width \"$instr\"");
  133. }
  134.  
  135. sub rhs_menu {
  136.     local ( $title, $message, $width, $numitems ) = @_;
  137.     local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
  138.  
  139.     shift; shift; shift; shift;
  140.  
  141.     @list = ();
  142.     for ($i = 0; $i < $numitems; $i++) {
  143.         $ent = shift;
  144.         $list[@list] = "\"$ent\"";
  145.     $ent = shift;
  146.         $list[@list] = "\"$ent\"";
  147.     }
  148.  
  149.     $message = &rhs_wordwrap($message, $width);
  150.  
  151.     $message_len = split(/^/, $message);
  152.     $tmp = $message;
  153.     if (chop($tmp) eq "\n") {
  154.     $message_len++;
  155.     }
  156.     
  157.     $height = $message_len + 6 + $numitems;
  158.     if ($height <= $scr_lines) {
  159.         $menuheight = $numitems;
  160.     } else {
  161.         $height = $scr_lines;
  162.         $menuheight = $scr_lines - $message_len - 6;
  163.     }
  164.  
  165.     return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
  166. }
  167.  
  168. sub rhs_menul {
  169.     local ( $title, $message, $width, $numitems ) = @_;
  170.     local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
  171.  
  172.     shift; shift; shift; shift;
  173.  
  174.     @list = ();
  175.     for ($i = 0; $i < $numitems; $i++) {
  176.         $ent = shift;
  177.         $list[@list] = "\"$ent\"";
  178.         $list[@list] = "\"\"";
  179.     }
  180.  
  181.     $message = &rhs_wordwrap($message, $width);
  182.  
  183.     $message_len = split(/^/, $message);
  184.     $tmp = $message;
  185.     if (chop($tmp) eq "\n") {
  186.     $message_len++;
  187.     }
  188.  
  189.     $height = $message_len + 6 + $numitems;
  190.     if ($height <= $scr_lines) {
  191.         $menuheight = $numitems;
  192.     } else {
  193.         $height = $scr_lines;
  194.         $menuheight = $scr_lines - $message_len - 6;
  195.     }
  196.  
  197.     return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
  198. }
  199.  
  200. sub rhs_menua {
  201.     local ( $title, $message, $width, %items ) = @_;
  202.     local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
  203.  
  204.     @list = ();
  205.     foreach $ent (sort keys (%items)) {
  206.         $list[@list] = "\"$ent\"";
  207.         $list[@list] = "\"$items{$ent}\"";
  208.     }
  209.  
  210.     $message = &rhs_wordwrap($message, $width);
  211.  
  212.     $message_len = split(/^/, $message);
  213.     $tmp = $message;
  214.     if (chop($tmp) eq "\n") {
  215.     $message_len++;
  216.     }
  217.  
  218.     $numitems = keys(%items);
  219.     $height = $message_len + 6 + $numitems;
  220.     if ($height <= $scr_lines) {
  221.         $menuheight = $numitems;
  222.     } else {
  223.         $height = $scr_lines;
  224.         $menuheight = $scr_lines - $message_len - 6;
  225.     }
  226.  
  227.     return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
  228. }
  229.  
  230. sub rhs_checklist {
  231.     local ( $title, $message, $width, $numitems ) = @_;
  232.     local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
  233.  
  234.     shift; shift; shift; shift;
  235.  
  236.     @list = ();
  237.     for ($i = 0; $i < $numitems; $i++) {
  238.         $ent = shift;
  239.         $list[@list] = "\"$ent\"";
  240.         $ent = shift;
  241.         $list[@list] = "\"$ent\"";
  242.         $ent = shift;
  243.     if ($ent) {
  244.         $list[@list] = "ON";
  245.     } else {
  246.         $list[@list] = "OFF";
  247.     }
  248.     }
  249.  
  250.     $message = &rhs_wordwrap($message, $width);
  251.  
  252.     $message_len = split(/^/, $message);
  253.     $tmp = $message;
  254.     if (chop($tmp) eq "\n") {
  255.     $message_len++;
  256.     }
  257.     
  258.     $height = $message_len + 6 + $numitems;
  259.     if ($height <= $scr_lines) {
  260.         $menuheight = $numitems;
  261.     } else {
  262.         $height = $scr_lines;
  263.         $menuheight = $scr_lines - $message_len - 6;
  264.     }
  265.  
  266.     return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
  267. }
  268.  
  269. sub rhs_checklistl {
  270.     local ( $title, $message, $width, $numitems ) = @_;
  271.     local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
  272.  
  273.     shift; shift; shift; shift;
  274.  
  275.     @list = ();
  276.     for ($i = 0; $i < $numitems; $i++) {
  277.         $ent = shift;
  278.         $list[@list] = "\"$ent\"";
  279.         $list[@list] = "\"\"";
  280.     $list[@list] = "OFF";
  281.     }
  282.  
  283.     $message = &rhs_wordwrap($message, $width);
  284.  
  285.     $message_len = split(/^/, $message);
  286.     $tmp = $message;
  287.     if (chop($tmp) eq "\n") {
  288.     $message_len++;
  289.     }
  290.     
  291.     $height = $message_len + 6 + $numitems;
  292.     if ($height <= $scr_lines) {
  293.         $menuheight = $numitems;
  294.     } else {
  295.         $height = $scr_lines;
  296.         $menuheight = $scr_lines - $message_len - 6;
  297.     }
  298.     return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
  299. }
  300.  
  301. sub rhs_checklista {
  302.     local ( $title, $message, $width, %items ) = @_;
  303.     local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
  304.  
  305.     shift; shift; shift; shift;
  306.  
  307.     @list = ();
  308.     foreach $ent (sort keys (%items)) {
  309.     $list[@list] = "\"$ent\"";
  310.     $list[@list] = "\"$items{$ent}\"";
  311.     $list[@list] = "OFF";
  312.     }
  313.  
  314.     $message = &rhs_wordwrap($message, $width);
  315.  
  316.     $message_len = split(/^/, $message);
  317.     $tmp = $message;
  318.     if (chop($tmp) eq "\n") {
  319.     $message_len++;
  320.     }
  321.     
  322.     $numitems = keys(%items);
  323.     $height = $message_len + 6 + $numitems;
  324.     if ($height <= $scr_lines) {
  325.         $menuheight = $numitems;
  326.     } else {
  327.         $height = $scr_lines;
  328.         $menuheight = $scr_lines - $message_len - 6;
  329.     }
  330.  
  331.     return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
  332. }
  333.  
  334. sub rhs_radiolist {
  335.     local ( $title, $message, $width, $numitems ) = @_;
  336.     local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
  337.  
  338.     shift; shift; shift; shift;
  339.  
  340.     @list = ();
  341.     for ($i = 0; $i < $numitems; $i++) {
  342.         $ent = shift;
  343.         $list[@list] = "\"$ent\"";
  344.         $ent = shift;
  345.         $list[@list] = "\"$ent\"";
  346.         $ent = shift;
  347.     if ($ent) {
  348.         $list[@list] = "ON";
  349.     } else {
  350.         $list[@list] = "OFF";
  351.     }
  352.     }
  353.  
  354.     $message = &rhs_wordwrap($message, $width);
  355.  
  356.     $message_len = split(/^/, $message);
  357.     $tmp = $message;
  358.     if (chop($tmp) eq "\n") {
  359.     $message_len++;
  360.     }
  361.  
  362.     $height = $message_len + 6 + $numitems;
  363.     if ($height <= $scr_lines) {
  364.         $menuheight = $numitems;
  365.     } else {
  366.         $height = $scr_lines;
  367.         $menuheight = $scr_lines - $message_len - 6;
  368.     }
  369.  
  370.     return &return_output(0 , "dialog --title \"$title\" --radiolist \"$message\" $height $width $menuheight @list");
  371. }
  372.  
  373. sub return_output {
  374.     local ( $listp, $command ) = @_;
  375.     local ( $res );
  376.  
  377.     open(SAVESTDERR, ">&STDERR");
  378.     open(STDERR, ">/tmp/dialogout");
  379.     $res = system($command);
  380.     close(STDERR);
  381.     open(STDERR, ">&SAVESTDERR");
  382.     
  383.     open(IN, "/tmp/dialogout");
  384.     if ($listp) {
  385.     @dialog_result = ();
  386.     while (<IN>) {
  387.         chop;
  388.         $dialog_result[@dialog_result] = $_;
  389.     }
  390.     } else {
  391.     $dialog_result = <IN>;
  392.     }
  393.     close(IN);
  394.     unlink("/tmp/dialogout");
  395.  
  396.     # Again, dialog returns results backwards
  397.     if (! $res) {
  398.     return 1;
  399.     } else {
  400.     return 0;
  401.     }
  402. }
  403.  
  404. sub rhs_wordwrap {
  405.     local ( $intext, $width ) = @_;
  406.     local ( $outtext, $i, $j, @lines, $wrap, @words, $pos, $pad );
  407.  
  408.     $outtext = "";
  409.     $pad = 3;            # leave 3 spaces around each line
  410.     $pos = $pad;        # current insert position
  411.     $wrap = 0;            # 1 if we have been auto wraping
  412.     $insert_nl = 0;        # 1 if we just did an absolute
  413.                 # and we should preface any new text
  414.                 # with a new line
  415.     @lines = split(/\n/, $intext);
  416.     for ($i = 0; $i <= $#lines; $i++) {
  417.         if ($lines[$i] =~ /^>/) {
  418.         $outtext .= "\n" if ($insert_nl);
  419.             $outtext .= "\n" if ($wrap);
  420.         $lines[$i] =~ /^>(.*)$/;
  421.             $outtext .= $1;
  422.         $insert_nl = 1;
  423.             $wrap = 0;
  424.             $pos = $pad;
  425.         } else {
  426.             $wrap = 1;
  427.             @words = split(/\s+/,$lines[$i]);
  428.             for ($j = 0; $j <= $#words; $j++) {
  429.         if ($insert_nl) {
  430.             $outtext .= "\n";
  431.             $insert_nl = 0;
  432.         }
  433.                 if ((length($words[$j]) + $pos) > $width - $pad) {
  434.                     $outtext .= "\n";
  435.                     $pos = $pad;
  436.                 }
  437.                 $outtext .= $words[$j] . " ";
  438.                 $pos += length($words[$j]) + 1;
  439.             }
  440.         }
  441.     }
  442.  
  443.     return $outtext;
  444. }
  445.  
  446. ############
  447. 1;
  448.